package com.changpei.search.controller;

import com.changpei.common.cache.UserCache;
import com.changpei.common.result.Result;
import com.changpei.search.pojo.UserIndex;
import com.changpei.search.service.UserIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *
 * <p>Title : UserIndex</p>
 * <p>Description : userIndex的控制层</p>
 * @author weibiao
 * @date 2020年3月7日
 */
@RestController
@RequestMapping("/userIndex")
@CrossOrigin
public class UserIndexController {

    @Autowired
    private UserIndexService userIndexService;

    @Autowired
    private UserCache userCache;

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增UserIndex数据</p>
     * @param userIndex
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody(required = false) UserIndex userIndex) {
        // 调用userIndexService实现添加UserIndex
        return userIndexService.save(userIndex);
    }

    /**
     *
     * <p>Title : delete</p>
     * <p>Description : UserIndex根据ID主键删除数据</p>
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable(required = false) Long id) {
        // 调用userIndexService实现根据主键删除
        return userIndexService.deleteById(id);
    }

    /**
     *
     * <p>Title : update</p>
     * <p>Description : UserIndex根据ID修改</p>
     * @param userIndex
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody UserIndex userIndex, @PathVariable Long id) {
        // 设置主键值
        userIndex.setId(id);
        // 调用userIndexService实现修改UserIndex
        return userIndexService.update(userIndex);
    }

    /**
     *
     * <p>Title : findById</p>
     * <p>Description : UserIndex分页搜索实现</p>
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<UserIndex> findById(@PathVariable Long id) {
        UserIndex userIndex = userIndexService.findById(id);
        return Result.ok(userIndex);
    }

    /**
     *
     * <p>Title : search</p>
     * <p>Description : UserIndex分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search")
    public Result<Map<String, Object>> userSearch(@RequestBody Map<String, Object> map) {
        // 调用userIndexService实现分页查询UserIndex
        Map<String, Object> result = userIndexService.userSearch(map);
        return Result.ok(result);
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @PostMapping(value = "/findUserInfo")
    public Result<UserIndex> findUserInfo(@RequestBody Map<String, Object> map) {
        UserIndex userIndex = userIndexService.findById(userCache.getUserId(map.get("token").toString()));
        return Result.ok(userIndex);
    }

    /**
     * 查询关注列表
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/followers")
    public Result<Page<UserIndex>> getFollowers(@RequestBody Map<String, Object> map) {
        String token = map.get("token").toString();
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        Long userId = userCache.getUserId(token);
        Page<UserIndex> followers = userIndexService.getFollowers(userId, pageNum, pageSize);
        return Result.ok(followers);
    }

    /**
     * 获取粉丝列表
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/fans")
    public Result<Map<String, Object>> getFans(@RequestBody Map<String, Object> map) {
        String token = map.get("token").toString();
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        Long userId = userCache.getUserId(token);
        Map<String, Object> fans = userIndexService.getFans(userId, pageNum, pageSize);
        return Result.ok(fans);
    }

    /**
     * 获取用户信息关注列表
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/user/followers")
    public Result<Map<String, Object>> getUserFollowers(@RequestBody Map<String, Object> map) {
        Map<String, Object> followers = userIndexService.getUserFollowers(map);
        return Result.ok(followers);
    }


    /**
     * 获取用户粉丝列表
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/user/fans")
    public Result<Map<String, Object>> getUserFans(@RequestBody Map<String, Object> map) {
        Map<String, Object> fans = userIndexService.getUserFans(map);
        return Result.ok(fans);
    }

}
